/*
 * Copyright (C) RT, 2017 by kiterunner_t
 * TO THE HAPPY FEW
 */

package com.zgys.mes.util.error;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Getter
public class RtResponse<T> {
    public static final RtResponse Success = new RtResponse(RtErrorEnum.SUCCESS);
    private static final int SuccessCode = RtErrorEnum.SUCCESS.getCode();
    private static final String SucessMessage = RtErrorEnum.SUCCESS.getMessage();

    private int code;
    private String message;
    private T content;



    public RtResponse(RtException ex) {
        message = ex.getMessage();
        code = ex.getCode();
    }


    public RtResponse(RtErrorEnum errorType) {
        code = errorType.getCode();
        message = errorType.getMessage();
    }

    public static<T> RtResponse setSuccessResponse(T data) {
        RtResponse restResponse =new RtResponse(RtErrorEnum.SUCCESS.getCode(), RtErrorEnum.SUCCESS.getMessage(),data);
        return restResponse;
    }


    public String toString() {
        String s;
        if (content == null) {
            s = String.format("{\"code\":%d,\"message\":\"%s\"}", code, message);
        } else {
            s = String.format("{\"code\":%d,\"message\":\"%s\",\"content\":%s}",
                              code, message, content.toString());
        }

        return s;
    }
}

